package com.xuzz.xrpc.spring.context.xml.schema;

import com.xuzz.xrpc.spring.context.AnnotatedBeanDefinitionRegistryUtils;
import com.xuzz.xrpc.spring.context.BootstrapApplicationListener;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;

/**
 * @description:
 * @author: xuzz
 * @create: 2020-02-21 17:35
 */
public class RPCNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        //注册listener
//        System.out.println("在RPCNamespaceHandler中注册BootstrapApplicationListener");
//        BeanDefinitionBuilder builder = rootBeanDefinition(BootstrapApplicationListener.class);
//        builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//        AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
//        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, parserContext.getRegistry());
        AnnotatedBeanDefinitionRegistryUtils.registerBeans(parserContext.getRegistry(),BootstrapApplicationListener.class);
        return super.parse(element, parserContext);
    }

    @Override
    public void init() {
        registerBeanDefinitionParser("server",new ServerElementParser());
        registerBeanDefinitionParser("service",new ServiceElementParser());
        registerBeanDefinitionParser("cluster",new ClusterElementParser());
        registerBeanDefinitionParser("reference",new ReferenceElementParser());
        registerBeanDefinitionParser("annotation",new AnnotationBeanDefinitionParser());
        registerBeanDefinitionParser("registry",new RegistryElementParser());
    }
}
